package com.shanling.shanlingcontroller.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.linkplay.lpvr.lpvrbean.interfaces.templateruntime.LPAVSWeatherForecast;
import com.linkplay.lpvr.lpvrbean.interfaces.templateruntime.LPAVSWeatherTemplate;
import com.shanling.shanlingcontroller.R;
import com.shanling.shanlingcontroller.adapter.CommonAdapter;
import com.shanling.shanlingcontroller.adapter.ViewHolder;
import com.shanling.shanlingcontroller.base.BaseAppCompatActivity;
import com.shanling.shanlingcontroller.bean.EventCenter;
import com.shanling.shanlingcontroller.bean.WeatherEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class WeatherActivity extends BaseAppCompatActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_date)
    TextView tvDate;
    @BindView(R.id.tv_laction)
    TextView tvLaction;
    @BindView(R.id.tv_currentWeather)
    TextView tvCurrentWeather;
    @BindView(R.id.tv_highTemperature)
    TextView tvHighTemperature;
    @BindView(R.id.tv_lowTemperature)
    TextView tvLowTemperature;
    @BindView(R.id.iv_weather)
    ListView ivWeather;
    private CommonAdapter<LPAVSWeatherForecast> weatherAdapter;
    private List<LPAVSWeatherForecast> weathers=new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void getBundleExtras(Bundle extras) {

    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_weather;
    }

    @Override
    protected void initViewsAndEvents() {
        weatherAdapter=new CommonAdapter<LPAVSWeatherForecast>(this,weathers,R.layout.item_weather) {
            @Override
            public void convert(ViewHolder helper, int position, LPAVSWeatherForecast item) {
                ImageView iv_currentWeatherIcon=helper.getView(R.id.iv_currentWeatherIcon);
                TextView tv_date=helper.getView(R.id.tv_date);
                TextView tv_temperature=helper.getView(R.id.tv_temperature);
                Glide.with(WeatherActivity.this).load(item.getImage().getUrl())
                        .into(iv_currentWeatherIcon);
                tv_date.setText(item.getDay());
                tv_temperature.setText(item.getHighTemperature()+"|"+item.getLowTemperature());
            }
        };
        ivWeather.setAdapter(weatherAdapter);


    }

    @Override
    protected void initData() {

    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected void onEventComing(EventCenter eventCenter) {

    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return true;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return TransitionMode.BOTTOM;
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEvent(WeatherEvent eventCenter) {
        if (eventCenter != null) {
            LPAVSWeatherTemplate weatherTemplate = eventCenter.getWeatherTemplate();
            if (weatherTemplate == null) {
                return;
            }
            tvDate.setText(weatherTemplate.getSubTitle());
            tvLaction.setText(weatherTemplate.getMainTitle());
            tvCurrentWeather.setText(weatherTemplate.getCurrentWeather());
            tvHighTemperature.setText(weatherTemplate.getHighTemperature());
            tvLowTemperature.setText(weatherTemplate.getLowTemperature());
            weatherAdapter.refresh(weatherTemplate.getForecastArray());
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        finish();
    }
}
